package com.shihb.chapter07designpattern.singleton

/**
 * Description:
 *
 * @author shihb
 * @date 2020/10/3 16:59
 */
object Test01 {
  def main(args: Array[String]): Unit = {
    val instance: SingleTon = SingleTon.getInstance
    val instance2: SingleTon = SingleTon.getInstance
    if(instance==instance2){
      println("相等")
    }
  }
}
//构造器私有化
class SingleTon private(){}
//懒汉式
object SingleTon{
  private var s:SingleTon=null
  def getInstance={
    if(s==null){
      s=new SingleTon
    }
    s
  }
}


//构造器私有化
class SingleTon2 private(){}
//饿汉式
object SingleTon2{
  private var s:SingleTon2=new SingleTon2
  def getInstance={
    s
  }
}


